import com.sun.source.tree.Tree;

public class Solution {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;

    public void  insert(int val) {
        if(root == null) {
            root = new TreeNode(val);
            return;
        }
        TreeNode node = new TreeNode(val);
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            if(cur.val == val) {
                return;
            }else if(cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else {
                parent = cur;
                cur = cur.right;
            }
        }
        if(val < parent.val) {
            parent.left = node;
        }else {
            parent.right = node;
        }
    }

    public void display(TreeNode root) {
        if(root == null) {
            return;
        }
        TreeNode cur = root;
        display(cur.left);
        System.out.print(cur.val + " ");
        display(cur.right);
    }

    public TreeNode prev;
    public TreeNode head;
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null) {
            return null;
        }
        Convert(pRootOfTree.left);
        if(prev == null) {
            prev = pRootOfTree;
            head = pRootOfTree;
        }else {
            prev.right = pRootOfTree;
            pRootOfTree.left = prev;
            prev = pRootOfTree;
        }
        Convert(pRootOfTree.right);
        return head;
    }

    public void display2(TreeNode root) {
        TreeNode cur = root;
        while(cur.left != null) {
            cur = cur.left;
        }
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.right;
        }
    }
}